/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/4/26
 * Time: 23:37
 */

#include "Sprite.h"

Texture::Texture(const std::string & file_path)
{
    texture = Game::GetInstance().GetAssetStore()->GetImage(file_path);
    SDL_GetTextureSize(texture, &src_rect.w, &src_rect.h);
}

Sprite * Sprite::AddSpriteChild(ObjectScreen * _parent, const std::string & _file_path, float _scale,Anchor _anchor)
{
    auto sprite = new Sprite();
    sprite->Init();
    sprite->SetAnchor(_anchor);
    sprite->SetTexture(Texture(_file_path));
    sprite->SetScale(_scale);
    sprite->SetParent(_parent);
    _parent->AddChild(sprite);

    return sprite;
}

void Sprite::Render()
{
    if(is_finish_)
        return;

    if(nullptr == texture_.texture)
        return;

    if(nullptr == parent_)
        return;

    auto pos = parent_->GetRenderPosition() + offset_;
    game_.RenderTexture(texture_, pos, size_);
}

void Sprite::SetTexture(const Texture &texture)
{
    texture_ = texture;
    size_ = glm::vec2(texture.src_rect.w, texture.src_rect.h);
}